package utils;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: dm
 * @date: 2023/1/31 14:00
 * @description:
 */
public class PDFUtils {
    /**
     * 读取pdf转换成图片
     * @param pdf    pdf路径
     * @param target 存储路径
     * @param suffix 后缀
     */
    public static void pdfToPic(String pdf, String target, String suffix) {
        try (FileInputStream is = new FileInputStream(pdf);
        ) {
            PDDocument pdDocument = PDDocument.load(is);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            int numberOfPages = pdDocument.getNumberOfPages();
            for (int i = 0; i < numberOfPages; i++) {
                OutputStream os = new FileOutputStream(target + "_" + i + "." + suffix);
                BufferedImage image = renderer.renderImageWithDPI(i, 100);
                ImageIO.write(image, suffix, os);
                os.flush();
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取pdf转换成图片，并合并图片
     * @param pdf    pdf路径
     * @param target 存储路径
     * @param suffix 后缀
     */
    public static void pdfToPicOne(String pdf, String target, String suffix) {
        try (FileInputStream is = new FileInputStream(pdf);
        ) {
            PDDocument pdDocument = PDDocument.load(is);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            int numberOfPages = pdDocument.getNumberOfPages();
            List<BufferedImage> list = new ArrayList<>();
            for (int i = 0; i < numberOfPages; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 100);
                list.add(image);
            }
            BufferedImage image1 = ImageUtils.mergeImage(false, list);
            OutputStream os = new FileOutputStream(target + "." + suffix);
            BufferedImage image = renderer.renderImageWithDPI(0, 100);
            ImageIO.write(image1, suffix, os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取pdf转换成word,粗糙
     * @param pdf    pdf路径
     * @param target 存储路径
     */
    public static void pdfToWord(String pdf, String target) {
        try (FileInputStream is = new FileInputStream(pdf);
             FileOutputStream fos = new FileOutputStream(target);
             PDDocument pdDocument = PDDocument.load(is);
             OutputStreamWriter writer = new OutputStreamWriter(fos,"UTF-8");
        ) {
            int numberOfPages = pdDocument.getNumberOfPages();
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setSortByPosition(true);
            stripper.setStartPage(1);
            stripper.setEndPage(numberOfPages);
            stripper.writeText(pdDocument, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 读取pdf转换成word,含水印
     * @param pdf    pdf路径
     * @param target 存储路径
     */
    public static void pdfToWordYes(String pdf, String target, FileFormat fileFormat) {
        try (FileInputStream is = new FileInputStream(pdf);
        ) {
            PdfDocument pdfDocument = new PdfDocument();
            pdfDocument.loadFromStream(is);
            pdfDocument.getConvertOptions().setConvertToWordUsingFlow(true);
            pdfDocument.saveToFile(target,fileFormat);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 读取pdf转换成word,去水印
     * @param pdf    pdf路径
     * @param target 存储路径
     */
    public static void pdfToWordGood(String pdf, String target, FileFormat fileFormat) {
        try (FileInputStream is = new FileInputStream(pdf);
             XWPFDocument xwpfDocument = new XWPFDocument()
        ) {
            PdfDocument pdfDocument = new PdfDocument();
            pdfDocument.loadFromStream(is);
            pdfDocument.getConvertOptions().setConvertToWordUsingFlow(true);
            List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
            for (XWPFParagraph paragraph : paragraphs) {
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    if (run.getText(0) != null) {
                        String text = run.getText(0).replaceFirst("Evaluation Warning : The document was created with Spire.PDF for java.", "");
                        run.setText(text, 0);
                    }
                }
            }
            xwpfDocument.write(new FileOutputStream(target));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取pdf内容
     * @param pdf
     */
    public static void readPdf(String pdf){
        try (FileInputStream is = new FileInputStream(pdf);
             PDDocument pdDocument = PDDocument.load(is)){
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setSortByPosition(true);
            String text = stripper.getText(pdDocument);
            System.out.println(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
